import concurrent.futures
import datetime

import numpy as np
import pandas as pd
from loguru import logger
from tqdm import tqdm

from components.config import WFQ_SOURCE_MYSQL_CONFIG
from utils.db.mysqldb import MysqlDB


class MsnObject:
    def __init__(self):
        self._to_db = None

    @property
    def to_db(self):
        if self._to_db is None:
            self._to_db = MysqlDB(
                ip=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_IP"],
                port=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_PORT"],
                db=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_DB"],
                user_name=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_USER_NAME"],
                user_pass=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_USER_PASS"],
            )
        return self._to_db

    def get_datas(self, mall_ids):
        time_str = datetime.datetime.now().replace(day=1).strftime("%Y-%m-%d")
        mall_ids_str = ",".join([f"'{i}'" for i in mall_ids])
        datas = self.to_db.find(
            f"select mall_id, request_url from net_pdd_proxy_log where mall_id in ({mall_ids_str})",
            to_json=True,
        )
        return pd.DataFrame(datas)

    def run_by_ids(self, mall_ids):
        logger.info(f"开始处理msn数据 {mall_ids}")
        df_pro = self.get_datas(mall_ids)
        self.parse_urls(df_pro)

    def parse_urls(self, df_pro):
        msn_df = df_pro[df_pro["request_url"].str.contains("msn=")].copy()
        if msn_df.empty:
            return
        df_new = msn_df.groupby("mall_id", as_index=False).apply(
            lambda df_one: self.parse_one_mall_url(df_one)
        )[["mall_id", "msn", "_sop_rcto"]]
        datas = df_new.to_dict("records")
        if datas:
            with concurrent.futures.ThreadPoolExecutor(max_workers=10) as t:
                for i in datas:
                    if i["msn"]:
                        t.submit(
                            self.to_db.update_smart,
                            "net_pdd_shop_info",
                            i,
                            condition=f"mall_id='{i['mall_id']}'",
                        )

    def parse_one_mall_url(self, df_one):
        if df_one.empty:
            return
        df_one["msn"] = df_one["request_url"].str.extract("msn=(.*?)&")
        df_one["_sop_rcto"] = df_one["request_url"].str.extract("_sop_rcto=(.*?)&")
        df_new = df_one[
            ~df_one["msn"].isnull()
            & ~(df_one["msn"] == "")
            & ~(df_one["msn"] == "null")
        ][["mall_id", "msn", "_sop_rcto"]].drop_duplicates(subset=["mall_id", "msn"])
        df_new = df_new.replace({np.NAN: None})
        return df_new

    def run(self):
        # self.run_by_ids(['102801257', '1052959', '109548654', '109744208', '110227140', '113055871', '1162131', '127326472', '1344961', '137158999', '148914418', '150845035', '153053849', '155772819', '1579290', '159639557', '160604502', '165021929', '167133830', '168310280', '171985035', '171985542', '172631443', '176252', '178725711', '178853225', '193880673', '2112689', '214756383', '216184680', '221163926', '233937425', '241083850', '241714443', '255735058', '258856090', '270287323', '273044251', '275670', '277964295', '282830319', '289535617', '292347788', '302487', '307146372', '312027922', '312057732', '312440770', '313386988', '320498585', '325634027', '329559630', '34704', '34912', '368551369', '372342636', '380098732', '384762166', '390651228', '391815233', '392684671', '397677310', '400117977', '403726930', '404700629', '405855347', '408087627', '411159572', '417903473', '420133403', '422095187', '422136761', '430780433', '432510090', '434245405', '436496245', '439264236', '440335380', '446942701', '447523633', '452351176', '458351840', '463309499', '466294367', '467203096', '477275200', '478279592', '480375139', '481948113', '495162675', '499463748', '503167905', '503851423', '519732056', '522240575', '530037683', '532784465', '535031061', '547497', '550000097', '561927144', '564777532', '570641', '588946259', '589564614', '599753375', '606737929', '615221894', '617292000', '6174793', '624241257', '628687852', '629644002', '630638885', '639623818', '644878911', '647028462', '64873', '654711083', '655809700', '657859495', '658889407', '659981801', '663636771', '700444752', '704718465', '717187992', '727735052', '729628449', '741038656', '741188058', '753112077', '766321916', '768965261', '773751359', '778298510', '785626454', '79310', '794263448', '798776281', '801381041', '804846685', '807033218', '814095310', '818924074', '820224351', '823034504', '828164483', '836691447', '844814633', '846317673', '852201080', '857710962', '857953051', '859368', '859511452', '860353685', '861285155', '862645243', '870362080', '880259866', '883347288', '891609659', '893573023', '894330131', '895697', '897255470', '898149549', '900441756', '900461101', '903765919', '905884144', '906365', '907641470', '911582073', '919983831', '92520', '939989313', '946314903', '950322630', '950843439', '952201398', '962242518', '963014980', '964620396', '965311658', '968528071', '969684852', '978314993', '982230599', '985850135', '992291648', '993233302', '100276', '101279564', '106164000', '106261223', '110507698', '111427129', '118873624', '121105576', '125729043', '126960421', '135671272', '136322802', '138545828', '139555963', '143321509', '151576463', '1540227', '156275126', '1564433', '160888525', '164094868', '167642921', '173332163', '1758437', '1762462', '176585435', '182146945', '182363870', '183361580', '183440374', '188956', '1920621', '192187124', '197315519', '197732383', '20179', '210584197', '215720616', '217958476', '223054681', '223060959', '2269898', '232559530', '232799179', '238913345', '243749142', '262111', '263802568', '266486406', '266635553', '268269827', '278459854', '279400415', '303917607', '304161727', '313119991', '315660249', '317229', '317562324', '321429366', '321889394', '321951047', '322693799', '324412502', '325146410', '326354643', '342849', '343068763', '351663277', '351922853', '356268991', '361034862', '361870906', '364887602', '367866371', '381475534', '3836157', '391148893', '392765890', '399108623', '401068998', '401634829', '410104963', '410773135', '421070342', '431462625', '439077386', '442724', '444896502', '453037374', '454601067', '461373842', '463013949', '466290479', '467143624', '467447799', '468142', '477634786', '479987501', '487240224', '490701901', '502121787', '505630951', '506638035', '507925666', '508069438', '515472', '518809803', '537377838', '542593181', '554739786', '555464205', '566432', '572118963', '578385917', '581352319', '583332298', '584086375', '589118387', '589523151', '592432692', '595859336', '602261763', '602355256', '604777113', '606114217', '614870631', '618414290', '620162067', '630326139', '638280670', '644564450', '647367545', '650025112', '651221824', '654281072', '655345856', '660577693', '661148', '663491003', '672058539', '681138739', '695825678', '697381194', '699058640', '700751374', '711999781', '717731938', '718963530', '726060824', '732765096', '733161099', '735129262', '739098965', '749539045', '757620824', '774421438', '786840516', '788204738', '794037275', '807536229', '811921893', '819029392', '819680735', '834325156', '834472881', '840970277', '842831281', '844426843', '846209537', '850367592', '857672901', '858198708', '865877302', '867474407', '867822435', '869492468', '874014294', '879883599', '885246768', '891383', '893710458', '901885396', '910200380', '918898539', '919317', '920203665', '926096562', '945697960', '961750525', '970835542', '974884779', '977009', '992608658', '997207647', '101457548', '101954658', '112421796', '1178672', '1198045', '125966482', '126023667', '127691522', '1283012', '136890254', '137209088', '137351489', '141370274', '144185328', '144333344', '147685744', '155716696', '1571818', '158986539', '164182', '175118315', '1775395', '1787697', '180972406', '181059155', '185549090', '198535013', '204232729', '2044177', '206440126', '206736799', '208088432', '208365578', '2119841', '212508269', '212981113', '2176880', '21910', '223995843', '226803143', '227891544', '237519053', '238501', '240466304', '244593602', '246847025', '248368809', '251605657', '253754', '256851265', '257407145', '261233433', '263453058', '269127568', '276212133', '277002', '294027', '295593784', '302010457', '316444222', '317762247', '321114247', '323113868', '324404242', '353051284', '376098664', '379222922', '381207422', '384859123', '388409837', '391378100', '394002410', '395858', '399889375', '403846763', '406674672', '407924032', '408375004', '409980263', '419591', '425159209', '448565058', '481281149', '481902326', '482313078', '484431309', '485173', '487502752', '491117386', '491483842', '506106625', '518594045', '539636445', '54288', '546913976', '550323432', '552786142', '558351070', '561361970', '563750767', '564201784', '56547', '565874276', '568462548', '577459448', '580750058', '580914743', '581537626', '583114899', '600073562', '601984182', '605319116'])
        # self.run_by_ids(["114005360"])
        mall_ids_dict = self.to_db.find(
            "select mall_id from net_pdd_shop_info where (msn is null or msn = '')", to_json=True
        )
        mall_ids = [i["mall_id"] for i in mall_ids_dict]
        batch_size = 1000
        task = []
        with concurrent.futures.ThreadPoolExecutor(max_workers=10) as t:
            for i in range(0, len(mall_ids), batch_size):
                batch_mall_ids = mall_ids[i:i + batch_size]
                task.append(t.submit(self.run_by_ids, batch_mall_ids))

        for j in tqdm(concurrent.futures.as_completed(task), total=len(task)):
            j.result()


if __name__ == "__main__":
    MsnObject().run()
